package com.api.oa.services.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.api.oa.common.RequestStatus;
import com.api.oa.mapper.YQAssessmentMapper;
import com.api.oa.mapper.YQRequestMapper;
import com.api.oa.mapper.YQUserMapper;
import com.api.oa.model.entity.YQAssessment;
import com.api.oa.model.entity.YQRequest;
import com.api.oa.model.vo.OaOrgTreeVO;
import com.api.oa.model.vo.YQRequestVO;
import com.api.oa.services.YQAssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class YQAssessmentServiceImpl implements YQAssessmentService {

    @Autowired
    private YQAssessmentMapper mapper;
    @Autowired
    private YQRequestMapper yqRequestMapper;

    @Autowired
    private YQUserMapper yqUserMapper;

    @Override
    public Integer add(YQAssessment entity) {
        return mapper.insert(entity);
    }

    @Override
    public void remove(Long id) {
        mapper.delete(id);
    }

    @Override
    public Integer update(YQAssessment entity) {
        return mapper.update(entity);
    }

    @Override
    public PageInfo<YQAssessment> list(String pid, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        PageHelper.orderBy("createdAt desc,userId desc");
        return new PageInfo<>(mapper.list(pid));
    }

    @Override
    public YQAssessment one(String id) {
        return mapper.findOne(id);
    }


    @Override
    @Transactional
    public Integer saveByIns(YQAssessment entity) throws Exception {
        Integer res = null;
        YQAssessment one = mapper.findOneByOrder(entity.getRequestOrder());
        YQRequestVO one1 = yqRequestMapper.findOne(entity.getRequestOrder());
        if (one == null) { //存入
            entity.setCreatedAt(DateUtil.date());
            entity.setPatientName(one1.getRequestName());
            entity.setSsCard(one1.getSsCard());
            entity.setIdentity(one1.getIdCardType());
            entity.setIdCard(one1.getRequestIdCard());
            entity.setInsUserId(one1.getInsUserId());
            entity.setCdad(one1.getConditionDesc());    //病情描述
            entity.setUserId(one1.getUserId());          //患者ID
            res = mapper.insert(entity);
            //更新一下用的身份证号码
            yqUserMapper.updateIDcard(one1.getUserId(),one1.getRequestIdCard());
        } else {   //更新
            if (one1.getStatus() != RequestStatus.INS_ASS_ING) {
                throw new Exception("申请单流程状态不对,不能修改");
            }
            entity.setUpdatedAt(DateUtil.date());
            res = mapper.updateByIns(entity);
        }
        if (entity.getIsCommit()) {  //进入下一环节
            yqRequestMapper.updateStatus(RequestStatus.GOV_ASS_ING, entity.getRequestOrder());
        }

        return res;
    }

    @Override
    @Transactional
    public YQRequestVO saveByGov(YQAssessment entity) throws Exception {
        Integer res = null;
        YQAssessment one = mapper.findOneByOrder(entity.getRequestOrder());
        if (one == null) {
            throw new Exception("申请单不存在!");
        }
        YQRequestVO one1 = yqRequestMapper.findOne(entity.getRequestOrder());
        if (one1.getStatus() != RequestStatus.GOV_ASS_ING) {
            throw new Exception("申请单流程状态不对,不能修改");
        }
        entity.setUpdatedAt(DateUtil.date());
        res = mapper.updateByGov(entity);

        if (entity.getIsCommit()) {  //进入下一环节
            yqRequestMapper.updateStatus(RequestStatus.PER_ASS_ING, entity.getRequestOrder());
        }
        return one1;
    }



    @Override
    public Integer submit(String id) {
        return  yqRequestMapper.updateStatus(RequestStatus.GOV_ASS_ING, id);
    }

    @Override
    public Integer submitGov(String id) {
        return yqRequestMapper.updateStatus(RequestStatus.PER_ASS_ING, id);
    }


}
